package com.socket.socket.demos.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.socket.socket.demos.common.BaseResponse;
import com.socket.socket.demos.common.ResultUtils;
import com.socket.socket.demos.domain.dto.environment.EnvironmentMonitorQueryRequest;
import com.socket.socket.demos.domain.model.EnvironmentMonitor;
import com.socket.socket.demos.domain.vo.EnvironmentMonitorVO;
import com.socket.socket.demos.service.EnvironmentMonitorService;
import com.socket.socket.demos.service.UserService;
import com.socket.socket.demos.socket.SocketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 环境检测参数接口
 *
 * @author <a href="https://github.com/liyupi">程序员lin</a>
 * @from <a href="https://www.code-nav.cn">编程导航学习圈</a>
 */
@RestController
@RequestMapping("/environmentMonitor")
@Slf4j
public class EnvironmentMonitorController {

    @Resource
    private EnvironmentMonitorService environmentMonitorService;

    @Resource
    private UserService userService;

    @Resource
    private SocketService socketService;



    /**
     * 分页获取环境检测参数列表（封装类）
     *
     * @param environmentMonitorQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<EnvironmentMonitorVO>> listEnvironmentMonitorVOByPage(@RequestBody EnvironmentMonitorQueryRequest environmentMonitorQueryRequest,
                                                               HttpServletRequest request) {
        long current = environmentMonitorQueryRequest.getCurrent();
        long size = environmentMonitorQueryRequest.getPageSize();
        // 查询数据库
        Page<EnvironmentMonitor> environmentMonitorPage = environmentMonitorService.page(new Page<>(current, size));
        List<EnvironmentMonitorVO> collect = environmentMonitorPage.getRecords()
                .stream().map(EnvironmentMonitorVO::objToVO).collect(Collectors.toList());
        Collections.reverse(collect);
        Page<EnvironmentMonitorVO> environmentMonitorVOPage = new Page<>(current, size);
        environmentMonitorVOPage.setRecords(collect);
        // 获取封装类
        return ResultUtils.success(environmentMonitorVOPage);
    }


    @GetMapping("/dooron")
    public BaseResponse<Boolean> doorOn(){
        log.info("门打开了");
        int doorStatus = 1;
        String flag = "door"+doorStatus+"";
        socketService.sendClient(flag);
        return ResultUtils.success(true);
    }
    @GetMapping("/dooroff")
    public BaseResponse<Boolean> doorOff(){
        log.info("门关上了");
        int doorStatus = 0;
        String flag = "door"+doorStatus+"";
        socketService.sendClient(flag);
        return ResultUtils.success(true);
    }
    @GetMapping("/lighton")
    public BaseResponse<Boolean> lightOn(){
        log.info("灯开了");
        int lightStatus = 1;
        String flag = "light"+lightStatus+"";
        socketService.sendClient(flag);
        return ResultUtils.success(true);
    }
    @GetMapping("/lightoff")
    public BaseResponse<Boolean> lightOff(){
        log.info("灯关了");
        int lightStatus = 0;
        String flag = "light"+lightStatus+"";
        socketService.sendClient(flag);
        return ResultUtils.success(true);
    }
    @GetMapping("/fanon")
    public BaseResponse<Boolean> fanOn(){
        log.info("风扇开了");
        int fanStatus = 1;
        String flag = "motor"+fanStatus+"";
        socketService.sendClient(flag);
        return ResultUtils.success(true);
    }
    @GetMapping("/fanoff")
    public BaseResponse<Boolean> fanOff(){
        log.info("风扇关了");
        int fanStatus = 0;
        String flag = "motor"+fanStatus+"";
        socketService.sendClient(flag);
        return ResultUtils.success(true);
    }
}
